import {Hardware} from './Hardware'
const ws = require('nodejs-websocket');

var first = require('rxjs/operators/first').first;
var filter = require('rxjs/operators/filter').filter;
var delay = require('rxjs/operators/delay').delay;

class ElectronServer extends Hardware {

    /* 生成说明文档 */
    generateDocument() {
        return `
       

        `
    }
    constructor(uid, config) {
        super(uid, config)
       
    }

    /* 重启所有进程  */
    restartAllSoftware() {
        let cp = this.child_process
        cp.exec("sudo supervisorctl reload", function (err, stdout, stderr) {
            if (err) {
                console.error(err);
            }
            console.log("stdout:", stdout)
            console.log("stderr:", stderr);
        });
    }

    shutdown() {
        let child_process = require("child_process");
        child_process.exec("shutdown now", function (err, stdout, stderr) {
            if (err) {
                console.error(err);
            }
            console.log("stdout:", stdout)
            console.log("stderr:", stderr);
        });
    }


    /* 配置使用sudo命令不用输入密码 */
    sudoWithoutPassword() {

    }

    mounted() {
        let that = this
        this._prepareSubject() //准备好主题
        this._bindListener()
        this._startSocketServer() //启动服务器

        this.dataRoot.$(this.dataRoot, 'onSocketText').valueChangeSubject
            .pipe(filter(param => param.newValue.path == "/handData")) // http请求来到此处
            .subscribe(function (param) {
                let msg = param.newValue
                let conn = that.childConnections['vue']

                let msgToSend = {
                    id: '',
                    path: '/handData',
                    query: {},
                    headers: {},
                    body: msg.body
                }
                conn.sendText(JSON.stringify(msgToSend))
            })
    }
}

export {ElectronServer}